from pywinauto.application import Application,findwindows
from pywinauto.keyboard import send_keys
from pywinauto import mouse
import os,time
from CheckFinsh import checkFinish
from functools import cmp_to_key

##快捷键打开 crtl+o

def getAllFile(path):
    res=[ name for name in os.listdir(path) if os.path.isfile(path + '/' + name)]
    res=sorted(res, key=cmp_to_key(lambda a, b: int(a.split(".")[0]) - int(b.split(".")[0]) )) #
    return res #[os.path.join(path,name) for name in res]


def sendNames(ind:str):
    res=[]
    for v in ind.split(".")[0]:
        res.append("{VK_NUMPAD%s}" % v)
    print(ind)
    print(res)
    return res

while True:
    print(checkFinish())
    path = input("请输入路径: ")
    for ind,pathFile in enumerate(getAllFile(path.strip())):
        app = Application().connect(path=r"C:\Program Files\Wondershare\喵影工厂（百度品牌推广PC）（CPC）\FilmoraCN.exe")
        app.万兴喵影.type_keys('^O')
        time.sleep(1)
        app["Dialog"]["Edit1"].TypeKeys(os.path.join(path,pathFile) )
        time.sleep(2)
        app["Dialog"]["Button1"].click()
        try:
            time.sleep(1)
            app["Dialog"]["Button1"].click()
        except:
            pass
        time.sleep(2)
        #print(findwindows.find_element(title=u'确定'))
        mouse.click(coords=(1037, 553))
        time.sleep(5)
        app = Application().connect(path=r"C:\Program Files\Wondershare\喵影工厂（百度品牌推广PC）（CPC）\FilmoraCN.exe")
        app.万兴喵影.type_keys('^E')
        time.sleep(1)
        send_keys("^a")
        send_keys("{DELETE}")
        for key in sendNames(pathFile):
            send_keys(key)
        time.sleep(3)
        mouse.click(coords=(1290, 703))
        time.sleep(10)
        while not checkFinish():
            print("还在转录中")
            time.sleep(5)
        mouse.click(coords=(1222, 608))
        time.sleep(10)

#app['另存为']['保存'].click()